package com.naughty.userlogin02.controller;

import com.alibaba.fastjson.JSON;
import com.naughty.userlogin02.bean.Finishwork;
import com.naughty.userlogin02.bean.QueryInfo;
import com.naughty.userlogin02.dao.FinishworkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class FinishworkController {

    @Autowired
    FinishworkDao finishworkDao;

    @CrossOrigin
    @RequestMapping("/allFinishwork")
    public String getFinishworkList(QueryInfo queryInfo) {

        System.out.println("hhhhhhhhhhhh");
        System.out.println(queryInfo);
        int numbers = finishworkDao.getFinishworkCounts("%" + queryInfo.getQuery() + "%");// 获取数据总数
        System.out.println("aaaaaa");
        System.out.println(numbers);

        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Finishwork> users = finishworkDao.getAllFinishwork("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers", numbers);
        res.put("data", users);
        System.out.println("总条数：" + numbers);
        String users_json = JSON.toJSONString(res);
        System.out.println("pppppp");
        System.out.println(users_json);
        return users_json;
    }

//    @RequestMapping("/addFinishwork")
//    public String addFinishwork(@RequestBody Finishwork finishwork) {
//        System.out.println("bbbbbbbbbbbb");
//        System.out.println(finishwork);
//        int i = finishworkDao.addFinishwork(finishwork);
//        String str = i > 0 ? "success" : "error";
//        System.out.println("bbaaaa");
//        System.out.println(i);
//        return str;//也可以通过这里返回报错
//    }

    @RequestMapping("/editFinishworkT")
    public String editFinishworkT(@RequestBody Finishwork finishwork) {
        System.out.println(finishwork);
        int i = finishworkDao.editFinishworkT(finishwork.getTid(),finishwork.getWid(),finishwork.getSid(),finishwork.getFwgrade(),finishwork.getFwcomment());
        System.out.println("我在批改");
        int j = finishworkDao.updateStateT(finishwork.getTid(),finishwork.getWid(),finishwork.getSid());
        System.out.println("批改好了");
        String str = i > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/editFinishworkS")
    public String editFinishworkS(@RequestBody Finishwork finishwork) {
        System.out.println(finishwork);
//        int i = finishworkDao.editFinishworkS(finishwork);
        int j = finishworkDao.updateStateS(finishwork.getTid(),finishwork.getWid(),finishwork.getSid());
        String str = j > 0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/countFinishwork")
    public int countFinishwork(@RequestBody Finishwork finishwork) {
        System.out.println(finishwork);
        int i = finishworkDao.countFinishwork(finishwork.getFwstate());
//        String str = i > 0 ? "success" : "error";
        return i;
    }

//    @RequestMapping("/getAllFinishwork")
//    public String getAllFinishwork1(@RequestBody Finishwork finishwork) {
//        System.out.println(finishwork);
//        int i = finishworkDao.getAllFinishwork(finishwork);
//        String str = i > 0 ? "success" : "error";
//        return str;
//    }
}

